﻿Public Class frmSearchCase

    Private Sub frmSearchCase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getHeaderGrid_Case()
        reset()
    End Sub

    Protected Sub getHeaderGrid_Case()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "សំណុំរឿង", "ប្រភេទសំណុំរឿង", "មេធាវី", "លេខសំណុំរឿង", "ស្ថាប័នតុលាការ", "លេខទូរស័ព្ទ", "តម្លៃសេវាកម្ម", "រឿងក្តីជោគជ័យ", "ថ្ងៃចាប់ផ្តើម", "អ្នកបញ្ចូល"}
        Dim HeaderWidth As Integer() = {100, 100, 300, 150, 150, 150, 200, 150, 150, 150, 150, 200}
        Dim i As Integer
        dgvData.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvData.Columns(i).HeaderText = HeaderText(i)
            dgvData.Columns(i).Width = HeaderWidth(i)
        Next
        dgvData.Columns(1).Visible = False
        dgvData.Columns(10).DefaultCellStyle.Format = "dd/MM/yyyy"
    End Sub

    Protected Sub getCase_All()
        dgvData.Rows.Clear()
        Dim oCase As New cCase
        Dim dt As New DataTable
        dt = oCase.select_Case_All()
        Dim dr As DataRow
        Dim i As Integer = 1
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7), dr(8), dr(9), dr(12))
            i += 1
        Next
    End Sub

    Protected Sub getCase_Finish()
        dgvData.Rows.Clear()
        Dim oCase As New cCase
        Dim dt As New DataTable
        dt = oCase.select_Case_Finish()
        Dim dr As DataRow
        Dim i As Integer = 1
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7), dr(8), dr(9), dr(12))
            i += 1
        Next
    End Sub

    Protected Sub getCase_NotFinish()
        dgvData.Rows.Clear()
        Dim oCase As New cCase
        Dim dt As New DataTable
        dt = oCase.select_Case_NotFinish()
        Dim dr As DataRow
        Dim i As Integer = 1
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7), dr(8), dr(9), dr(12))
            i += 1
        Next
    End Sub

    Protected Sub reset()
        txtSearch.Clear()
        txtSearch.Focus()
        rdCaseNotFinish.Checked = True
        rdCaseFinish.Checked = False
        rdCaseAll.Checked = False
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub rdCaseFinish_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdCaseNotFinish.CheckedChanged
        RadioCase_CheckedChanged()
    End Sub

    Private Sub rdCaseNotFinish_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdCaseFinish.CheckedChanged
        RadioCase_CheckedChanged()
    End Sub

    Private Sub rdCaseAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdCaseAll.CheckedChanged
        RadioCase_CheckedChanged()
    End Sub

    Protected Sub RadioCase_CheckedChanged()
        If rdCaseNotFinish.Checked Then
            getCase_NotFinish()
        ElseIf rdCaseFinish.Checked Then
            getCase_Finish()
        Else
            getCase_All()
        End If
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        If rdCaseNotFinish.Checked Then
            getCase_NotFinish_byKeyword()
        ElseIf rdCaseFinish.Checked Then
            getCase_Finish_byKeyword()
        Else
            getCase_All_byKeyword()
        End If
    End Sub

    Protected Sub getCase_All_byKeyword()
        dgvData.Rows.Clear()
        Dim oCase As New cCase
        Dim dt As New DataTable
        dt = oCase.select_Case_All_byKeyword(txtSearch.Text)
        Dim dr As DataRow
        Dim i As Integer = 1
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7), dr(8), dr(9), dr(12))
            i += 1
        Next
    End Sub

    Protected Sub getCase_Finish_byKeyword()
        dgvData.Rows.Clear()
        Dim oCase As New cCase
        Dim dt As New DataTable
        dt = oCase.select_Case_Finish_byKeyword(txtSearch.Text)
        Dim dr As DataRow
        Dim i As Integer = 1
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7), dr(8), dr(9), dr(12))
            i += 1
        Next
    End Sub

    Protected Sub getCase_NotFinish_byKeyword()
        dgvData.Rows.Clear()
        Dim oCase As New cCase
        Dim dt As New DataTable
        dt = oCase.select_Case_NotFinish_byKeyword(txtSearch.Text)
        Dim dr As DataRow
        Dim i As Integer = 1
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(1), dr(2), dr(3), dr(4), dr(5), dr(6), dr(7), dr(8), dr(9), dr(12))
            i += 1
        Next
    End Sub


    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        If dgvData.Rows.Count > 0 Then
            frmCaseIncome.cboCase.SelectedValue = dgvData.CurrentRow.Cells(1).Value
            Close()
        End If
    End Sub

    Private Sub dgvData_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvData.DoubleClick
        btnChoose_Click(Nothing, Nothing)
    End Sub
End Class